package com.cgbl.cg.ui.vip

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.cgbl.cg.databinding.AppFragmentShopBinding
import com.cgbl.cg.vm.main.ShopFragmentViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.cgbl.cg.R
import com.cgbl.cg.adapter.*
import com.robot.base.configs.PrefsManager
import com.robot.base.model.CourseBean
import com.robot.base.model.ResourceBean
import com.robot.base.utils.pic.ImageLoaderUtils
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import me.goldze.mvvmhabit.BR

class VipFragment : GLBaseFragment<AppFragmentShopBinding, ShopFragmentViewModel>() {

    private lateinit var vipMenuAdapter: MainVipMenuAdapter

    private lateinit var recommendCourseAdapter: VipRecommendCourseAdapter

    private lateinit var allCourseAdapter: VipAllCourseAdapter

    private lateinit var popularMaterialAdapter: VipAllResourceAdapter

    private lateinit var vipHotResourceAdapter: VipHotResourceAdapter

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_shop

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initView()
        initBanner()
    }

    override fun onResume() {
        super.onResume()
        viewModel.initUserInfo()//PrefsManager.getUserInfo().vipType == "1"
    }

    override fun initData() {
        super.initData()
        viewModel.initData()
    }

    private fun initView() {
        vipMenuAdapter = MainVipMenuAdapter()
        binding.giftMenuList.apply {
            layoutManager = GridLayoutManager(context, 3)
            adapter = vipMenuAdapter
        }

        val menus = mutableListOf(
            VipMenuBean(R.mipmap.ic_main_vip_menu1, "随便学", "全站VIP免费学"),
            VipMenuBean(R.mipmap.ic_main_vip_menu2, "VIP标识", "专属VIP标识"),
            VipMenuBean(R.mipmap.ic_main_vip_menu3, "专属礼包", "内部资源10G专享"),
            VipMenuBean(R.mipmap.ic_main_vip_menu4, "专属服务", "专属服务更便捷"),
            VipMenuBean(R.mipmap.ic_main_vip_menu5, "专享折扣", "单付费课程专享折扣"),
            VipMenuBean(R.mipmap.ic_main_vip_menu6, "海量素材", "海量素材随便下"),
        )

        vipMenuAdapter.setNewInstance(menus)


        recommendCourseAdapter = VipRecommendCourseAdapter()
        binding.hotCourseList.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = recommendCourseAdapter
        }


        allCourseAdapter = VipAllCourseAdapter()
        binding.allCourseList.apply {
            layoutManager = GridLayoutManager(context, 2)
            adapter = allCourseAdapter
        }

        popularMaterialAdapter = VipAllResourceAdapter()
        binding.allResourceList.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = popularMaterialAdapter
        }
    }


    private fun initBanner() {
        vipHotResourceAdapter = VipHotResourceAdapter(arrayListOf())
        binding.bannerResource.setAdapter(
            vipHotResourceAdapter
        )
        binding.bannerResource.setBannerGalleryEffect(84, 0)
        binding.bannerResource.start()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.recommendCourseDataEvent.observe(this) {
            recommendCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
        }

        viewModel.uc.allCourseDataEvent.observe(this) {
            allCourseAdapter.setNewInstance(it as MutableList<CourseBean>?)
        }

        viewModel.uc.allResourceDataEvent.observe(this) {
            popularMaterialAdapter.setNewInstance(it as MutableList<ResourceBean>?)
        }

        viewModel.uc.recommendResourceDataEvent.observe(this) {
            vipHotResourceAdapter.setDatas(it)
        }
    }
}